home *** CD-ROM | disk | FTP | other *** search
- #include <Fonts.h>
- #include <LowMem.h>
- #include <NumberFormatting.h>
- #include <Processes.h>
-
- #include <Movies.h>
-
- Boolean getMovieFile(FSSpec *theFile);
-
- OSErr OpenSelection(FSSpecPtr theDocToOpen);
-
- Boolean fPeriodic( TimeValue srcMovieDuration, TimeValue timeNow, StringPtr fileName, StringPtr srcCompression, StringPtr dstCompression);
- void fPStrCopy( Str255 s1, Str255 s2 );
- void fPStrConcat( Str255 s1, Str255 s2 );
- void fSetRGBColor( RGBColor *theColor, short r, short g, short b );
- void fPNumToString( float theNum, Str255 theString, short fix );
-
- void main(void)
- {
- OSErr err;
- FSSpec file;
- Movie sourceMovie;
- short resFref;
- WindowPtr w;
- Rect bounds;
- ImageDescriptionHandle srcDesc = (ImageDescriptionHandle)NewHandle(0);
- ImageDescriptionHandle dstDesc = (ImageDescriptionHandle)NewHandle(0);
- Track srcVideoTrack, dstVideoTrack;
- Media srcVideoMedia, dstVideoMedia;
- Handle nextSample = NewHandle(0);
- Handle srcSample = NewHandle(0);
- long nextSampleSize, srcSampleSize;
- Movie dstMovie = nil;
- short dstResRef = -1;
- TimeValue timeNow = 0;
- TimeValue mediaTime;
- TimeValue sampleDuration;
- Handle dataRef = nil;
- OSType dataRefType;
- ImageTranscodeSequence its;
- short outputFref = 0;
- long fileOffset;
- TimeValue srcMovieDuration;
- Str31 srcCompression;
- FSSpec newFile;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- MaxApplZone();
-
- EnterMovies();
-
- if (!getMovieFile(&file))
- return;
-
- SetRect(&bounds, 75, 75, 75+160,75+120);
- w = NewCWindow(nil, &bounds, file.name, false, 0, (WindowPtr)-1,
- true, 0);
- if (!w) return;
-
- SetPort(w);
-
- err = OpenMovieFile(&file, &resFref, fsRdPerm);
- if (err) return;
-
- err = NewMovieFromFile(&sourceMovie, resFref, nil, (StringPtr)nil,
- newMovieActive, nil);
- if (err) return;
-
- CloseMovieFile(resFref);
-
- srcVideoTrack = GetMovieIndTrackType(sourceMovie, 1, VideoMediaType, movieTrackMediaType);
- if (!srcVideoTrack) return;
-
- newFile = file;
- newFile.name[++newFile.name[0]] = '!';
- err = CreateMovieFile(&newFile, 'TVOD', -1, createMovieFileDeleteCurFile, &dstResRef, &dstMovie);
- if (err) return;
-
- GetMovieDefaultDataRef(dstMovie, &dataRef, &dataRefType);
-
- err = AddEmptyTrackToMovie(srcVideoTrack, dstMovie, dataRef, dataRefType, &dstVideoTrack);
- if (err) return;
-
- DisposeHandle(dataRef);
-
- err = FSpOpenDF(&newFile, fsRdWrPerm, &outputFref);
- if (err) Debugger();
-
- err = SetFPos(outputFref, fsFromLEOF, 0);
- if (err) Debugger();
-
- err = GetEOF(outputFref, &fileOffset);
- if (err) Debugger();
-
- dstVideoMedia = GetTrackMedia(dstVideoTrack);
- srcVideoMedia = GetTrackMedia(srcVideoTrack);
- GetMediaSampleDescription(srcVideoMedia, 1, (SampleDescriptionHandle)srcDesc);
-
- BlockMoveData((**srcDesc).name, srcCompression, 32);
-
- err = ImageTranscodeSequenceBegin(&its, srcDesc, 'mjpb', &dstDesc);
- if (err) Debugger();
-
- srcMovieDuration = GetMovieDuration(sourceMovie);
-
- while (!fPeriodic(srcMovieDuration, timeNow, file.name, srcCompression, (StringPtr)"\pMotion JPEG Format B")) {
- TimeValue mediaTime = TrackTimeToMediaTime(timeNow, srcVideoTrack);
- void *dstData;
- long dstDataSize;
-
- err = GetMediaSample(srcVideoMedia, srcSample, 0, &srcSampleSize, mediaTime, nil, &sampleDuration,
- (SampleDescriptionHandle)srcDesc, nil, 1, nil, nil);
- if (err) Debugger();
-
- HLock(srcSample);
- err = ImageTranscodeFrame(its, *srcSample, srcSampleSize, &dstData, &dstDataSize);
- if (err) Debugger();
-
- HUnlock(srcSample);
-
- err = FSWrite(outputFref, &dstDataSize, dstData);
- if (err) Debugger();
-
- ImageTranscodeDisposeFrameData(its, dstData);
-
- err = AddMediaSampleReference(dstVideoMedia, fileOffset, dstDataSize, sampleDuration, (SampleDescriptionHandle)dstDesc, 1, 0, nil);
- if (err) Debugger();
-
- fileOffset += dstDataSize;
-
- GetTrackNextInterestingTime(srcVideoTrack, nextTimeMediaSample, timeNow, 1, &timeNow, nil);
- if (timeNow == -1)
- break;
- }
-
- FSClose(outputFref);
- outputFref = 0;
-
- err = InsertMediaIntoTrack(dstVideoTrack, 0, 0, GetMediaDuration(dstVideoMedia), 0x010000);
- if (err) Debugger();
-
- AddMovieResource(dstMovie, dstResRef, nil, nil);
- CloseMovieFile(dstResRef);
-
- ImageTranscodeSequenceEnd(its);
- DisposeMovie(sourceMovie);
- DisposeMovie(dstMovie);
- DisposeHandle(nextSample);
- DisposeHandle((Handle)srcDesc);
- DisposeHandle((Handle)dstDesc);
-
- OpenSelection(&newFile);
- }
-
-
- /*==================================================================================*/
- Boolean fPeriodic( TimeValue srcMovieDuration, TimeValue timeNow, StringPtr fileName, StringPtr srcCompression, StringPtr dstCompression) {
- /* Check for events, update progress bar. Return true to quit program. */
-
- #define kTimeStamps 10
-
- long i;
- Boolean quit;
- EventRecord theEvent;
- RGBColor theColor;
- float framesPerSecond;
- long startTick;
- long endTick;
- Str255 theString;
- WindowRef aWindow;
- static gSetup;
- static Rect gTheRect;
- static Rect gTextRect;
- static Rect gFileRect;
- static Rect gSrcCompressionRect, gDstCompressionRect;
- static Rect gFillRect;
- static long gTimeStamps[kTimeStamps];
- static long gNextStamp;
- static WindowPtr gWindow = nil;
-
- quit = false;
-
- /* Check for events */
- if( OSEventAvail( mDownMask | keyDownMask, &theEvent) ) {
- GetNextEvent(mDownMask | keyDownMask, &theEvent);
- switch( theEvent.what ) {
- case mouseDown:
- if (FindWindow( theEvent.where, &aWindow ) == inGoAway) {
- if (TrackGoAway(aWindow, theEvent.where))
- quit = true;
- }
- break;
- case keyDown:
- if( theEvent.modifiers & cmdKey )
- if( (char)theEvent.message == 'Q' ||
- (char)theEvent.message == 'q' ||
- (char)theEvent.message == '.' )
- quit = true;
- break;
- }
- }
-
- /* Draw progress the first time */
- if( !gSetup ) {
- gWindow = GetNewCWindow(128, nil, (WindowPtr)-1);
- SetPort(gWindow);
-
- gTheRect = gWindow->portRect;
- gTheRect.left += 20;
- gTheRect.bottom -= 20;
- gTheRect.top = gTheRect.bottom - 20;
- gTheRect.right -= 80;
- gTextRect = gTheRect;
- OffsetRect( &gTextRect, 0, -40 );
- gFileRect = gTheRect;
- OffsetRect( &gFileRect, 0, -100 );
- gSrcCompressionRect = gTheRect;
- OffsetRect( &gSrcCompressionRect, 0, -80 );
- gDstCompressionRect = gTheRect;
- OffsetRect( &gDstCompressionRect, 0, -60 );
- FrameRect( &gTheRect );
- InsetRect( &gTheRect, 1, 1 );
- gFillRect = gTheRect;
- if( gWindow->portBits.rowBytes < 0 )
- if( (*((CGrafPtr)gWindow)->portPixMap)->pixelSize > 1 ) {
- fSetRGBColor( &theColor, 205, 205, 255 );
- RGBForeColor( &theColor );
- PaintRect( &gFillRect );
- fSetRGBColor( &theColor, 0, 0, 0 );
- RGBForeColor( &theColor );
- }
-
- for( i = 0; i < kTimeStamps; i++ )
- gTimeStamps[i] = -1;
- gNextStamp = 0;
-
- MoveTo( gTextRect.left, gTextRect.bottom );
- DrawString( "\pFrames per second: " );
- gTextRect.left += StringWidth( "\pFrames per second: " );
-
- MoveTo( gFileRect.left, gFileRect.bottom );
- DrawString( "\pSource file: " );
- TextFace(bold);
- DrawString( fileName );
- TextFace(normal);
-
- MoveTo( gSrcCompressionRect.left, gSrcCompressionRect.bottom );
- DrawString( "\pOriginal format: " );
- TextFace(bold);
- DrawString( srcCompression );
- TextFace(normal);
-
- MoveTo( gDstCompressionRect.left, gDstCompressionRect.bottom );
- DrawString( "\pTarget format: " );
- TextFace(bold);
- DrawString( dstCompression );
- TextFace(normal);
-
- gSetup = true;
- }
-
- /* Update progress the rest of the time */
- else {
-
- gFillRect.right = gTheRect.left + (gTheRect.right - gTheRect.left) *
- timeNow / (float)srcMovieDuration;
- fSetRGBColor( &theColor, 86, 86, 86 );
- RGBForeColor( &theColor );
- PaintRect( &gFillRect );
- fSetRGBColor( &theColor, 0, 0, 0 );
- RGBForeColor( &theColor );
- TextMode(srcCopy);
-
- startTick = gTimeStamps[gNextStamp];
- endTick = TickCount();
- gTimeStamps[gNextStamp++] = endTick;
- if( gNextStamp == kTimeStamps )
- gNextStamp = 0;
- if( startTick != -1 ) {
- framesPerSecond = kTimeStamps / (float)(endTick - startTick) * 60.0;
- fPNumToString( framesPerSecond, theString, 2 );
- MoveTo( gTextRect.left, gTextRect.bottom );
- TextFace(bold);
- DrawString( theString );
- DrawString( "\p " );
- }
- }
-
- return( quit );
- }
-
- /*==================================================================================*/
- void fSetRGBColor( RGBColor *theColor, short r, short g, short b ) {
-
- theColor->red = r * 256;
- theColor->green = g * 256;
- theColor->blue = b * 256;
- }
-
- /*==================================================================================*/
- void fPNumToString( float theNum, Str255 theString, short fix ) {
-
- short i;
- Boolean negative;
- long wholeNum;
- float fraction;
- long fractNum;
- Str255 fractString;
- short padCount;
-
- negative = false;
- if( theNum < 0 ) {
- negative = true;
- theNum = -theNum;
- }
-
- /* Round the number off before proceeding */
- fraction = 0.500001;
- for( i = 0; i < fix; i++ )
- fraction /= 10;
- theNum += fraction;
-
- /* Set up whole number portion of the string */
- wholeNum = theNum;
-
- /* Set up fractional portion of string */
- fraction = theNum - wholeNum;
- for( i = 0; i < fix; i++ )
- fraction *= 10;
- fractNum = fraction;
-
- /* Build the string */
- NumToString( wholeNum, theString );
- theString[++theString[0]] = '.';
- NumToString( fractNum, fractString );
- padCount = fix - fractString[0];
- for( i = 0; i < padCount; i++ )
- theString[++theString[0]] = '0';
- fPStrConcat( theString, fractString );
-
- /* Strip trailing zero's (except the one after the decimal place if any) */
- while( theString[theString[0]] == '0' &&
- theString[theString[0] - 1] != '.' &&
- theString[0] > 3 )
- theString[0]--;
-
- if( negative ) {
- BlockMove( &theString[1], &theString[2], theString[0] );
- theString[1] = '-';
- theString[0]++;
- }
- }
-
- /*==================================================================================*/
- void fPStrCopy( Str255 s1, Str255 s2 ) {
- /* Copies pascal string s2 into s1 (bowing to strcpy() parameter ordering). */
-
- BlockMove( s2, s1, 255 < s2[0] + 1 ? 255 : s2[0] + 1 );
- }
-
- /*==================================================================================*/
- void fPStrConcat( Str255 s1, Str255 s2 ) {
- /* Attaches pascal string s2 to pascal string s1. */
-
- short i;
-
- i = 255 - s1[0] < s2[0] ? 255 - s1[0] : s2[0];
- BlockMove( &s2[1], &s1[s1[0] + 1], i );
- s1[0] += i;
- }
-
- /*==================================================================================*/
-
- pascal OSErr AEOpenDoc(AppleEvent *message, AppleEvent *reply,long refCon);
- OSErr MissedAEParameters (AppleEvent *message);
-
- Boolean getMovieFile(FSSpec *theFile)
- {
- Boolean result = false;
- unsigned long gStartTicks = LMGetTicks();
- void *upp = NewAEEventHandlerProc(AEOpenDoc);
-
- theFile->vRefNum = 0;
-
- AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, upp, (long)theFile, false);
-
- while (gStartTicks + 30 > (unsigned long)LMGetTicks()) {
- EventRecord event;
-
- WaitNextEvent(everyEvent, &event, 0, 0);
- if (event.what == kHighLevelEvent)
- AEProcessAppleEvent(&event);
- }
-
- AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments, upp, false);
-
-
- if (theFile->vRefNum == 0) {
- StandardFileReply reply;
- SFTypeList types;
-
- types[0] = MovieFileType;
- StandardGetFilePreview(nil, 1, types, &reply);
-
- result = reply.sfGood;
- if (result)
- *theFile = reply.sfFile;
- }
- else
- result = true;
-
- return result;
- }
-
- pascal OSErr AEOpenDoc(AppleEvent *message, AppleEvent *reply,long refCon)
- {
- FSSpec fss;
- AEDescList docList;
- long index, itemsInList;
- Size actualSize;
- AEKeyword keywd;
- DescType typeCode;
- OSErr err;
- FSSpec *filePtr = (FSSpec *)refCon;
-
- err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
- if(err) goto bail;
-
- err = MissedAEParameters(message);
- if(err) goto bail;
-
- err = AECountItems(&docList, &itemsInList);
- if(err) goto bail;
-
- for (index = 1; index <= itemsInList; index++) {
- HFileInfo pb;
-
- err = AEGetNthPtr(&docList, index, typeFSS, &keywd, &typeCode,
- (Ptr)&fss, sizeof(FSSpec), &actualSize);
- if(err) goto bail;
-
- // need to figure out if this is a directory or a file
-
- pb.ioVRefNum = fss.vRefNum;
- pb.ioFDirIndex = 0;
- pb.ioDirID = fss.parID;
- pb.ioNamePtr = fss.name;
- err = PBGetCatInfoSync((void *)&pb);
- if (!err) {
- if (pb.ioFlAttrib & 16) {
- fss.parID = pb.ioDirID;
- }
- else {
- *filePtr = fss;
- break;
- }
- }
- }
-
- err = AEDisposeDesc(&docList);
-
- bail:
- return err;
- }
-
- OSErr MissedAEParameters (AppleEvent *message)
- {
- DescType typeCode;
- Size actualSize;
- OSErr err;
-
- err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeWildCard,
- &typeCode, nil, 0L, &actualSize);
- if (err == errAEDescNotFound)
- return(noErr);
- return(err = noErr ? errAEEventNotHandled : err);
- }
-
-
- OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN);
-
- /* OpenSelection takes a FSSpec pointer, and creates a Finder Open Selection */
- /* AppleEvent for the document described by the FSSpec. This can be an */
- /* application or document. */
- OSErr OpenSelection(FSSpecPtr theDocToOpen)
- {
- OSErr err;
- AppleEvent aeEvent;
- AEDesc myAddressDesc, aeDirDesc, listElem;
- FSSpec dirSpec;
- AEDesc fileList;
- ProcessSerialNumber process;
- AliasHandle DirAlias = nil, FileAlias = nil;
-
- /* go find the Finder's process information, please */
- if (FindAProcess('FNDR', 'MACS', &process))
- return procNotFound;
-
- myAddressDesc.dataHandle = nil;
- aeDirDesc.dataHandle = nil;
- listElem.dataHandle = nil;
- fileList.dataHandle = nil;
- aeEvent.dataHandle = nil;
-
- /* Create an address descriptor so the AppleEvent manager knows where to send this event */
- if (err = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process), &myAddressDesc))
- goto bail;
-
- /* Create the empty FinderEvent */
- /* it's a Finder 'FNDR', Open Selection 'sope' event */
- if (err = AECreateAppleEvent('FNDR', 'sope', &myAddressDesc, kAutoGenerateReturnID, kAnyTransactionID, &aeEvent))
- goto bail;
-
- /* make a FSSpec for the parent folder (see the OpenSelection description in the AE Registry ) */
- /* using the information in the document FSSpec */
- if (err = FSMakeFSSpec(theDocToOpen->vRefNum, theDocToOpen->parID, nil, &dirSpec))
- goto bail;
- if (err = NewAliasMinimal(&dirSpec, &DirAlias))
- goto bail;
-
- /* Create alias for file */
- if (err = NewAliasMinimal(theDocToOpen, &FileAlias))
- goto bail;
-
- /* Create the file list */
- if (err = AECreateList(nil, 0, false, &fileList))
- goto bail;
-
- /* create the folder descriptor */
- HLock((Handle)DirAlias);
- err = AECreateDesc(typeAlias, (void *)*DirAlias, GetHandleSize((Handle)DirAlias), &aeDirDesc);
- if (err) goto bail;
- DisposeHandle((Handle)DirAlias);
- DirAlias = nil;
-
- /* put the Directory Desc in the event as the direct object */
- if ((err = AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) == noErr) {
- AEDisposeDesc(&aeDirDesc);
- aeDirDesc.dataHandle = nil;
-
- /* create the file descriptor and add to aliasList */
- HLock((Handle)FileAlias);
- err = AECreateDesc(typeAlias, (void *)*FileAlias, GetHandleSize((Handle)FileAlias), &listElem);
- if (err) goto bail;
- DisposeHandle((Handle)FileAlias);
- FileAlias = nil;
-
- err = AEPutDesc(&fileList, 0, &listElem);
- if (err) goto bail;
- }
-
- AEDisposeDesc(&listElem);
- listElem.dataHandle = nil;
-
- /* Add the file alias list to the event */
- if (err = AEPutParamDesc(&aeEvent, 'fsel', &fileList))
- goto bail;
-
- AEDisposeDesc(&fileList);
- fileList.dataHandle = nil;
-
- /* And now send the event! */
- err = AESend(&aeEvent, nil, kAENoReply | kAENeverInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
- if (err == noPortErr) {
- err = AESend(&aeEvent, nil, kAENoReply | kAENeverInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
- }
-
- bail:
- AEDisposeDesc(&aeEvent);
- AEDisposeDesc(&myAddressDesc);
- AEDisposeDesc(&aeDirDesc);
- AEDisposeDesc(&listElem);
- AEDisposeDesc(&fileList);
- AEDisposeDesc(&aeEvent);
- DisposeHandle((Handle)DirAlias);
- DisposeHandle((Handle)FileAlias);
-
- return err;
- }
-
-
- OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN)
- {
- OSErr err;
- ProcessInfoRec tempInfo;
-
- processSN->lowLongOfPSN = kNoProcess;
- processSN->highLongOfPSN = kNoProcess;
-
- tempInfo.processInfoLength = sizeof(ProcessInfoRec);
- tempInfo.processName = nil;
- tempInfo.processAppSpec = nil;
-
- do {
- if (err = GetNextProcess(processSN))
- break;
-
- GetProcessInformation(processSN, &tempInfo);
- } while ((tempInfo.processSignature != creatorToFind) && (tempInfo.processType != typeToFind));
-
- return err;
- }
-